package com.css.prompt.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.css.prompt.model.PromptTemplate;
import com.css.prompt.service.PromptTemplateService;

import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "Prompt Template 管理接口")
@RestController
@RequestMapping("/prompt/templates")
public class PromptTemplateController {

    private final PromptTemplateService service;

    public PromptTemplateController(PromptTemplateService service) {
        this.service = service;
    }

    @GetMapping
    public List<PromptTemplate> getAll() {
        return service.getAll();
    }

    @GetMapping("/{id}")
    public PromptTemplate getById(@PathVariable Long id) {
        return service.getById(id).orElseThrow(() -> new RuntimeException("模板不存在"));
    }

    @PostMapping
    public PromptTemplate create(@RequestBody PromptTemplate template) {
        return service.save(template);
    }

    @PutMapping("/{id}")
    public PromptTemplate update(@PathVariable Long id, @RequestBody PromptTemplate template) {
        return service.update(id, template);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        service.delete(id);
    }
    
    /**
     * 分页获取所有提示词
     * 示例：GET /api/my-prompts?page=0&size=10&sort=date,desc
     */
    @GetMapping("page")
    public Page<PromptTemplate> getPage(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "date,desc") String[] sort
    ) {
        // 构建 Sort 对象（支持多个字段）
        Sort sortObj = Sort.by(
            Arrays.stream(sort)
                  .map(s -> {
                      String[] parts = s.split(",");
                      return parts.length == 2
                              ? new Sort.Order(Sort.Direction.fromString(parts[1]), parts[0])
                              : new Sort.Order(Sort.Direction.ASC, s);
                  })
                  .toList()
        );

        Pageable pageable = PageRequest.of(page, size, sortObj);
        return service.getPage(pageable);
    }

    /**
     * 搜索分页
     * 示例：GET /api/my-prompts/search?keyword=博客&page=0&size=10
     */
    @GetMapping("/search")
    public Page<PromptTemplate> search(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        Pageable pageable = PageRequest.of(page, size, Sort.by("date").descending());
        return service.searchByName(keyword, pageable);
    }
}

